package org.csu.logistics.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.logistics.common.CommonResponse;
import org.csu.logistics.entity.Order;
import org.csu.logistics.persistence.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("OrderService")
public class OrderServiceImp implements OrderService{

    @Autowired
    OrderMapper orderMapper;

    @Override
    public void insert_order(Order order){
        orderMapper.insert(order);
    }

    @Override
    public void delete_orderByOrderNum(int orderNum){
        orderMapper.deleteById(orderNum);
    }

    @Override
    public void update_order(Order order){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ordernum",order.getOrderNum());
        orderMapper.update(order,queryWrapper);
    }

    @Override
    public Order select_orderByOrderNum(int orderNum){
        Order order = orderMapper.selectById(orderNum);
        return order;
    }

    @Override
    public List<Order> select_orderList(){
        return orderMapper.selectList(null);
    }

    @Override
    public List<Order> select_orderListByUserId(int userId){

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userId);
        List<Order> orderList = orderMapper.selectList(queryWrapper);
        if(orderList.isEmpty()){
            return null;
        }

        else return orderList;
    }

    @Override
    public Order select_orderByUserId(int userId){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userId);
        List<Order> orderList = orderMapper.selectList(queryWrapper);
        Order order = orderList.get(orderList.size()-1);
        return order;
    }

    @Override
    public List<Order> select_orderListByOrderStatus(String orderStatus){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderstatus",orderStatus);
        return orderMapper.selectList(queryWrapper);
    }

}
